/*
 * Project Name:hbm-be.
 * File Name:CsSmNoticeServiceImpl.java
 * Package Name:com.hginfo.cs.be.service.impl.cs
 * Date:2018年04月24日 下午3:58:49
 * Copyright (c) 2016, hongguaninfo.com All Rights Reserved.
 *
 */
package cn.uce.sms.srv.biz.impl;

import cn.uce.base.page.Page;
import cn.uce.base.page.Pagination;
import cn.uce.sms.api.service.biz.ICsSmNoticeBiz;
import cn.uce.sms.entity.CsSmNotice;
import cn.uce.sms.srv.service.CsSmNoticeService;
import cn.uce.sms.vo.CsSmNoticeVo;
import cn.uce.web.common.base.CurrentUser;
import cn.uce.web.common.util.WebUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 公告表: cs_sm_notice. <br />
 * service impl 层 <br />
 * Date: 2018年04月24日 下午3:58:49 <br />
 *
 * @author you
 * @since V1.0.0
 */
@Service("csSmNoticeBiz")
@Transactional(readOnly = true, propagation= Propagation.SUPPORTS, rollbackFor = Exception.class)
public class CsSmNoticeBiz implements ICsSmNoticeBiz {

	/**
	 * @author QGH
	 */
	@Resource
	private CsSmNoticeService csSmNoticeService;

	/**
	 * 获取未查看的公告
	 */
	@Override
	public List<CsSmNoticeVo> getNewCsSmNotice() {
		return csSmNoticeService.getNewCsSmNotice(WebUtil.getCurrentUser().getEmpCode());
	}

	/**
	 * 公告列表
	 *
	 * @param csSmNoticeVo
	 * @param page
	 * @return
	 */
	@Override
	public Pagination<CsSmNoticeVo> getList(CsSmNoticeVo csSmNoticeVo, Page page) {
		return csSmNoticeService.getList(csSmNoticeVo, page);
	}

	/**
	 * 通过主键id获取公告详情
	 *
	 * @param id 公告列表的主键id
	 * @return
	 */
	@Override
	public CsSmNotice findById(Long id) {
		return csSmNoticeService.findById(id);
	}

	/**
	 * 通过主键删除公告
	 * @param id
	 * @return
	 */
	@Override
	public int deleteById(Long id) {
		return csSmNoticeService.deleteById(id);
	}

	/**
	 * 公告的发布或者取消
	 * @param idStr
	 * @param isPublish
	 * @return
	 */
	@Override
	public int updateById(String idStr, Integer isPublish) {
		Session shiroSession = SecurityUtils.getSubject().getSession(true);
		Integer publishPeople = ((CurrentUser) shiroSession.getAttribute(cn.uce.web.common.constants.Constants.CURRENT_USER)).getEmpId();
		CsSmNotice csSmNotice = new CsSmNotice();
		if(isPublish == 1){//发布公告
			csSmNotice.setPublishPeople(publishPeople.longValue());
		}

		int num = 0;
		List<Long> idList = new ArrayList<Long>();
		String[] idArr = idStr.split(",");
		for(String id : idArr){
			idList.add(Long.valueOf(id));
		}
		for (Long id : idList){
			csSmNotice.setId(id);
			csSmNotice.setStatus(isPublish);
			if(isPublish == 1){//发布公告
				Date publishTime = new Date();
				csSmNotice.setPublishTime(publishTime);
			}
			csSmNoticeService.updateById(csSmNotice);
			num ++;
		}
		return num;
	}

	@Override
	public CsSmNotice seeCsSmNotice(Long id) {
		return csSmNoticeService.seeCsSmNotice(id);
	}
}
